package xyz.icxl.android.architecture.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;

import androidx.appcompat.widget.AppCompatTextView;

import com.blankj.utilcode.util.StringUtils;

import xyz.icxl.android.architecture.R;

/**
 * 文件描述：倒计时按钮
 * <p>
 * 文件创建人：ChenXinLei
 * <p>
 * 文件创建人联系方式：502616659(QQ)
 * <p>
 * 创建时间：2020 年 08月 24 日
 * <p>
 * 文件版本：v1.0
 * <p>
 * 版本描述：创建文件
 */
public class CountdownTextView extends AppCompatTextView {
    public  int currentState = -1;
    private int minVal = 0;
    private int maxVal = 60;
    private  int currentVal = 60;
    private CountdownButtonListener listener;

    public void setListener(CountdownButtonListener listener) {
        this.listener = listener;
    }

    /**
     * 设置最大秒数
     *
     * @param maxVal
     */
    public void setMaxVal(int maxVal) {
        this.maxVal = maxVal;
    }

    public void reset() {
        currentState = -1;
        currentVal = 60;
        maxVal = 60;
        minVal = 0;
        init();
    }

    public CountdownTextView(Context context) {
        super(context);
        init();
    }

    @SuppressLint("SetTextI18n")
    private void init() {
        if (currentState != -1 && currentVal > 0) {
            setText(StringUtils.getString(R.string.verification_code_countdown_template, currentVal));
        }
        setGravity(Gravity.CENTER);
        lookCurrentVal();
    }

    public CountdownTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CountdownTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void start() {
        if (currentState != -1) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (currentState == -1) return;
                    if (currentVal > minVal) {
                        setCurrentVal(currentVal - 1);
                        start();
                    } else {
                        currentState = -1;
                        setCurrentVal(maxVal);
                    }
                }
            }, 1000);
        }
    }

    private void lookCurrentVal() {
        if (currentState != -1) {
            new Handler().postDelayed(new Runnable() {
                @SuppressLint("SetTextI18n")
                @Override
                public void run() {
                    if (currentVal > minVal && currentState != -1) {
                        setText(StringUtils.getString(R.string.verification_code_countdown_template, currentVal));
                        lookCurrentVal();
                    } else {
                        setText(getContext().getString(R.string.get_code));
                        setEnabled(true);
                        if (listener != null) {
                            listener.onComplete();
                        }
                    }
                }
            }, 1000);
        }
    }


    public synchronized  void setCurrentVal(int currentVal) {
        this.currentVal = currentVal;
    }

    public void onResume() {
        lookCurrentVal();
    }

    public void go() {
        if (currentState == -1) {
            currentState = 0;
            start();
            setEnabled(false);
            lookCurrentVal();
        }
    }

    public interface CountdownButtonListener {
        /**
         * 完成
         */
        void onComplete();
    }

}
